package top.kaoshanji.p1ch04a;

import org.apache.hc.core5.http.ContentType;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;

/**
 * 代码清单-p1ch04a-89页a：取得初始化参数名
 * URL访问地址：/initSnoop
 * @author kaoshanji
 * ^_^
 * create time 2025/4/3 16:09
 */
public class InitSnoop extends GenericServlet {

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        // 设置请求对象的编码
        req.setCharacterEncoding(StandardCharsets.UTF_8.name());

        // 设置响应对象的编码和内容格式
        res.setContentType(ContentType.TEXT_PLAIN.toString());
        PrintWriter out = res.getWriter();

        out.println("Init Parameters:");
        Enumeration<String> enumeration = getInitParameterNames();
        while (enumeration.hasMoreElements()) {
            String name = enumeration.nextElement(); // 初始化参数名
            out.println("初始化参数名称：" + name + ": 初始化参数值：" + getInitParameter(name)); // 通过参数名获得参数值
        }

    }

}
